/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.client.cv.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import es.sonxurxo.gpsgame.client.R;
import es.sonxurxo.gpsgame.client.cv.activity.LoginActivity;
import es.sonxurxo.gpsgame.client.model.service.to.Game;
import es.sonxurxo.gpsgame.client.model.service.to.Player;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class WaitUntilGameStartService extends Service
{
	private NotificationManager mNM;
	//    private PendingGamesDbAdapter p = new PendingGamesDbAdapter(this);
	private Player player;
	private Game game;
	Thread thr;

	@Override
	public void onCreate() {

	}

	@Override
	public void onStart(Intent intent, int startId) {

		player = (Player)intent.getExtras().getSerializable("player");
		game = (Game)intent.getExtras().getSerializable("game");
		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);


		// Start up the thread running the service.  Note that we create a
		// separate thread because the service normally runs in the process's
		// main thread, which we don't want to block.
		thr = new Thread(null, mTask, "GameToStartService");    
		thr.start();
	}

	Runnable mTask = new Runnable() {
		@SuppressWarnings("static-access")
		public void run() {

			long sleepTime = game.getStartDate().getTimeInMillis() - 
			System.currentTimeMillis();

			try {
				if (sleepTime>0)
					thr.sleep(sleepTime);
			} catch (InterruptedException e) {
				return;
			}
			// show the icon in the status bar
			showNotification("Its the time for game!");
			// Done with our work...  stop the service!
			WaitUntilGameStartService.this.stopSelf();
		}
	};

	@Override
	public void onDestroy() {
		//    	thr.stop();
		thr.interrupt();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	/**
	 * Show a notification while this service is running.
	 */
	private void showNotification(String txt) {

		// Set the icon, scrolling text and timestamp
		Notification notification = new Notification(R.drawable.logo, txt,
				System.currentTimeMillis());

		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.audioStreamType = AudioManager.STREAM_ALARM;
		notification.defaults = Notification.DEFAULT_SOUND;

		Intent i = new Intent(this, LoginActivity.class);
		i.putExtra("player", player);

		// The PendingIntent to launch our activity if the user selects this notification
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this, "Please " + 
				player.getLogin() + ", click to start game!",
				txt, contentIntent);

		// Send the notification.
		// We use a string id because it is a unique number.  We use it later to cancel.
		mNM.notify(R.string.cancel, notification);
	}
}

